package com.jfinal_demo.app.services;

import com.jfinal.kit.Kv;
import com.jfinal.plugin.activerecord.Page;
import com.jfinal_demo.app.model.Artical;

public class ArticalService extends BaseService {
    public Artical createArtical(String title, String content, long tagId, long userId) {
        Artical artical = new Artical();
        artical.setContent(content);
        artical.setTagId(tagId);
        artical.setTitle(title);
        artical.setUserId(userId);
        artical.save();
        return artical;
    }

    public void modifyArtical(long id, String title, String content, long tagId, long userId) {
        Artical artical = Artical.dao.findById(id);
        if (artical == null) throw new RuntimeException("内容不存在");
        if (artical.getUserId() != userId) throw new RuntimeException("无权修改他人内容");

        artical.setContent(content);
        artical.setTagId(tagId);
        artical.setTitle(title);
        artical.setUserId(userId);
        artical.update();
    }

    public Page<Artical> list(Long tagId, int pageNumber, int pageSize) {
        Kv kv = new Kv();
        if (tagId != null) kv.set("tagId", tagId);
        return page(Artical.dao, "artical.count", "artical.list", kv, pageNumber, pageSize);
    }

    public boolean deleteArtical(long articalId) {
        return Artical.dao.deleteById(articalId);
    }
}
